<template>
  <el-container>
    <el-aside width="55%"></el-aside>
    <el-main>
      <el-form
        :model="registerForm"
        status-icon
        :rules="rules"
        ref="registerForm"
        label-width="100px"
        class="register-form"
      >
        <el-form-item> <h1 style="font-size: 24px">会员注册</h1> </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model="registerForm.username"
            autocomplete="off"
            placeholder="账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="registerForm.password"
            autocomplete="off"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input
            type="password"
            v-model="registerForm.checkPassword"
            autocomplete="off"
            placeholder="确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()" style="width: 100%"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button @click="$router.push('/login')" style="width: 100%"
            >登陆</el-button
          >
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import { SPACE, insert } from '@/api/baseApi'
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerForm.checkPassword !== '') {
          this.$refs.registerForm.validateField('checkPassword')
        }
        callback()
      }
    }
    var validateCheckPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: '',
        password: '',
        checkPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePassword, trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, validator: validateCheckPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      const params = { username: this.registerForm.username, password: this.registerForm.password }
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          insert(SPACE.user, params).then(res => {
            if (res.state === 200) {
              this.showSuccess('注册成功')
              this.$router.push('/login')
            } else {
              this.showError(res.msg)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.el-container {
  background: url("../assets/imgs/login-bgc.jpg") no-repeat;
  background-size: 100% 100%;
}
.el-main {
  margin: 0 auto;
  background: none;
}
.register-form {
  border-radius: 12px;
  margin: 65px auto;
  padding: 20px 70px 20px 0px;
  width: 58%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  background-color: #fff;
  text-align: center;
}
</style>
